/**
 * @file main.cpp
 */

 #include <getopt.h>
#include <iostream>
#include <qapplication.h>
#include <unistd.h>
using namespace std;

#include <defines.h>
#include <Xados.hpp>
#include <ConfigManager.hpp>

int main(int argc, char** argv) {
	string configName = "";

	{
		static struct option long_options[] = {
				{"help",	false,	0,	'h'},
				{"version",	false,	0,	'v'},
				{"editor",	false,	0,	'e'},
				{"external",	true,	0,	'x'},
				{NULL,		false,	NULL,	0}
		};

		int opt;
		int idx = 0;
		string	shortOpts = "";
		for(int i=0;long_options[i].name!=NULL;i++) {
			shortOpts+=long_options[i].val;
			if(long_options[i].has_arg) {
				shortOpts+=":";
			}
		}
		while((opt = getopt_long(argc, argv, shortOpts.c_str(), long_options, &idx)) != -1) {
			switch(opt) {
				case 'h':
					///@todo
					cout << "SHOW HELP" << endl;
					break;
				case 'v':
					///@todo
					cout << "SHOW VERSION" << endl;
					break;
				case 'e':
					///@todo
					cout << "LAUNCH EDITOR" << endl;
					break;
				case 'x':
					///@todo
					configName = optarg;
					break;
				default: /* '?' */
					cerr << XADOS_PROGRAM": Worng parameter '" << (char)opt << "'." << endl;
			}
		}
	}

	if(configName != "") {
		ConfigManager* config = new ConfigManager(configName);
		if(config->isLoaded()) {
			QApplication app(argc, argv);

			Xados* xados = new Xados(config);
			xados->show();
			app.exec();
			delete xados;
			delete config;
		} else {
			cerr << XADOS_PROGRAM": Configuration for '" << configName << "' was not successfully loaded." << endl;
		}
	} else {
		cerr << XADOS_PROGRAM": No configuration was specified." << endl;
	}

	return 0;
}
